import { showModal, showToast } from "../../utils/asyncWx"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        address: {},
        cart: [],
        allChececked: false,
        totalPrice: 0,
        totalNum: 0
    },
    // 点击添加收货地址
    handleChooseAddress() {
        wx.chooseAddress({
            success: (result) => {
                wx.setStorage({
                    key: 'address',
                    data: result
                });

            }
        });


    },
    // 商品选中
    handleItemChange(e) {
        //获取商品id
        const goods_id = e.currentTarget.dataset.id;
        // 购物车
        let { cart } = this.data;
        //找到商品
        const index = cart.findIndex(v => v.goods_id == goods_id);
        cart[index].checked = !cart[index].checked;

        this.setCart(cart);


    },
    // 设置购物车状态 计算价格
    setCart(cart) {
        let allChececked = true;
        //总价格 总数量
        let totalPrice = 0;
        let totalNum = 0;
        cart.forEach(v => {
            if (v.checked) {
                totalPrice += v.num * v.goods_price;
                totalNum += v.num
            } else {
                allChececked = false;
            }
        });
        wx.setStorageSync("cart", cart);
        //判断数组是否为空
        allChececked = cart.length != 0 ? allChececked : false;
        //重置
        this.setData({
            cart,
            totalPrice,
            totalNum,
            allChececked
        })

    },
    //全选
    handleChangeAllCheck() {
        //获取数据1
        let { cart, allChececked } = this.data;
        allChececked = !allChececked;
        //循环修改cart的值
        cart.forEach(v => v.checked = allChececked);
        //重置
        this.setData({
            cart,
            allChececked
        })
    },
    //数量编辑
    async handleItemNumEdit(e) {
        //获取参数
        const { operation, id } = e.currentTarget.dataset;
        // 购物车数组
        let { cart } = this.data;
        //找到索引
        const index = cart.findIndex(v => v.goods_id === id);
        //判断是否删除
        if (cart[index].num === 1 && operation === -1) {
            //弹窗提示
            const res = await showModal({ content: '您是否要删除？' });
            if (res.confirm) {
                cart.splice(index, 1);
                this.setCart(cart);
            }
        } else {
            // 修改数量
            cart[index].num += operation;
            //重置
            this.setCart(cart);
        }

    },
    // 支付功能
    async handlePay() {
        //判断收获地址
        const { address, totalNum } = this.data;
        if (!address) {
            await showToast({ title: "您还没有添加收货地址" })
            return;
        }
        // 判断是否选购商品
        if (totalNum === 0) {
            await showToast({ title: "您还没有选购商品" })
            return;
        }
        // 跳转支付页面
        wx.navigateTo({
            url: '/pages/pay/index',
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        //得到缓存中的地址
        let address = wx.getStorageSync("address");
        let cart = wx.getStorageSync("cart") || [];
        if (address != '') {
            address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
        }
        this.setData({
            address
        })
        this.setCart(cart)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})